package com.general.template.controller;

import com.general.template.dto.QuestionCommonDto;
import com.general.template.enums.ResultEnum;
import com.general.template.export.entity.ImportParams;
import com.general.template.export.handler.ExcelImportHandler;
import com.general.template.pojo.PowerJobs;
import com.general.template.pojo.PowerQuestionCommon;
import com.general.template.pojo.PowerUser;
import com.general.template.service.PowerQuestionCommonService;
import com.general.template.utils.IdWorker;
import com.general.template.utils.Pages;
import com.general.template.utils.Result;
import com.general.template.vo.QuestionCommonVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 杰哥
 * Date: 2021/2/16 22:39
 * @Description:
 **/
@RequestMapping("questionCommon")
@RestController
@Api(value="共性题库管理",description = "对共性题库管理的查看和修改")
public class QuestionCommonController {

    @Autowired
    private IdWorker worker;

    @Autowired
    private PowerQuestionCommonService questionCommonService;

    /**
     * 导入题目信息
     *
     * @author 杰哥
     * @date 2021/2/17 15:52
     * @param file
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "importAdd",method = RequestMethod.POST)
    @ApiOperation(value="导入excel文件",notes = "批量导入用户信息")
    @ApiImplicitParam(name = "file",value = "上传文件，",paramType = "formData",required = true,dataType = "file")
    public Result<Object> importAdd(MultipartFile file){
        List<QuestionCommonVo> list = null;
        InputStream inputStream = null;
        Map<String, Object> map = null;
        ExcelImportHandler importHandler = new ExcelImportHandler();
        try {
            inputStream = file.getInputStream();
            list = importHandler.importExcel(inputStream, QuestionCommonVo.class, new ImportParams(0,10));
            map = questionCommonService.addList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<>(ResultEnum.SUCCESS,map);
    }

    /**
     * 添加单个题目信息
     *
     * @author 杰哥
     * @date 2021/2/17 15:52
     * @param common
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "addQuestion",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="添加单个题目",notes = "添加单个题目信息")
    public Result<Object> addQuestionCommon(@RequestBody PowerQuestionCommon common){
        common.setId(Long.valueOf(worker.nextId()).toString());
        common.setQuestionType("单选题");
        common.setCreateTime(new Date());
        int add = questionCommonService.addQuestion(common);
        if (add==0){
            return new Result<>(ResultEnum.SUCCESS,"添加失败!题目信息有误!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"添加成功!");
        }
    }

    /**
     * 修改题目信息
     *
     * @author 杰哥
     * @date 2021/2/17 15:55
     * @param common
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "updateInfo",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改题目",notes = "修改题目信息")
    public Result<Object> updateInfo(@RequestBody PowerQuestionCommon common){
        if (common.getQuestionMediaStatus()==0){
            common.setQuestionMediaName("");
            common.setQuestionMediaUrl("");
        }
        boolean byId = questionCommonService.updateById(common);
        if (!byId){
            return new Result<>(ResultEnum.SUCCESS,"修改失败!该题目不存在");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"修改成功!");
        }
    }

    /**
     * 禁用题目
     *
     * @author 杰哥
     * @date 2021/2/17 15:57
     * @param id
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="禁用题目",notes = "禁用题目")
    public Result<Object> delete(@PathVariable("id") String id){
        PowerQuestionCommon common = new PowerQuestionCommon();
        common.setId(id);
        common.setStatus(1);
        boolean b = questionCommonService.updateById(common);
        if (b){
            return new Result<>(ResultEnum.SUCCESS,"禁用成功!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"禁用失败!");
        }
    }

    /**
     * 启用题目
     *
     * @author 杰哥
     * @date 2021/2/17 15:57
     * @param id
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "enable/{id}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="启用题目",notes = "启用题目")
    public Result<Object> enable(@PathVariable("id") String id){
        PowerQuestionCommon common = new PowerQuestionCommon();
        common.setId(id);
        common.setStatus(0);
        boolean b = questionCommonService.updateById(common);
        if (b){
            return new Result<>(ResultEnum.SUCCESS,"启用成功!");
        }else{
            return new Result<>(ResultEnum.SUCCESS,"启用失败!");
        }
    }

    /**
     * 后台管理分页查询
     *
     * @author 杰哥
     * @date 2021/2/17 17:04
     * @param pages
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="分页查询",notes = "分页查询用户")
    public Result<Object> page(@RequestBody Pages<QuestionCommonDto<String>> pages){
        Pages<QuestionCommonDto<String>> pageList = questionCommonService.pageList(pages);
        return new Result<>(ResultEnum.SUCCESS,pageList);
    }

    /**
     * 通用题库获取
     *
     * @author 杰哥
     * @date 2021/2/17 17:05
     * @return com.general.template.utils.Result<java.lang.Object>
     */
    @RequestMapping(value = "getQuestionList/{jobId}",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="通用题库获取",notes = "通用题库获取")
    public Result<Object> getQuestionList(@PathVariable("jobId") Integer jobId){
        List<QuestionCommonDto> list = questionCommonService.getRandomList(jobId);
        return new Result<>(ResultEnum.SUCCESS,list);
    }

}
